﻿using System;
using System.Collections.Generic;
using System.Text;
using Kitware.VTK;
using System.ComponentModel;

namespace Fu3DEngine
{
   public  class TerrainNode:NodeBase
   {
       #region "对外暴露的属性系统"
       [DisplayName("颜色表")]
       public LookupTable pLt { get; set; }

       public void InitProps()
       {
 
       }
       #endregion


       TerrainInfo m_Info;
       PolyDataAtom m_TerrainMain;
       PolyDataAtom m_OutLine;
       TexturePlyAtom m_Img;//映射图纸


       TextureInfo m_TInfo; //这个只是用来暂时指导切割的坐标

       public TerrainNode(string id)
           : base(id)
       {
           m_Info = new TerrainInfo();

           m_TerrainMain = new PolyDataAtom();

           m_OutLine = new PolyDataAtom();

           m_Img = new TexturePlyAtom();


          
 
       }


       public override void  Run()
        {

             base.Run();
             this.PipeLine();
             this.InitProps();

        }

       public TerrainInfo InitByDem(string strDem)
       {
          

           m_Info.SetImageData( DataSource.GetImageFromDem(strDem));

           return m_Info;

           //double z=  m_Info.GetBaseHeight(411670, 137081);

           
 
       }

       public TerrainInfo InitByPlane()
       {


           return m_Info;
 
       }

       public void PipeLine()
       {
           this.CreateTerrain();
           this.CreateOutLine();
          
          // this.CreateWidget();
              
 
       }

       public override void ReLoad()
       {
           throw new NotImplementedException();
       }

       public override void Unload()
       {
           base.Unload();

       }


 


       public void CreateWidget()
       {

           //下面就是完成一些乱起八遭的功能，暂时性的放在这里

           
           //下面的代码值是个测试
           vtkImageData data = m_Info.imgData;

          
           //主地形的添加



           vtkImageDataGeometryFilter gemo = vtkImageDataGeometryFilter.New();
           gemo.SetInput(data);
           gemo.OutputTrianglesOn();


           vtkWarpScalar m_warp = vtkWarpScalar.New();
           m_warp.SetInput(gemo.GetOutput());

           m_warp.SetScaleFactor(1);
           m_warp.SetNormal(0, 0, 1);
           m_warp.UseNormalOn();

           m_warp.Update();
      





           // Create a text property for both cube axes
           vtkTextProperty tprop;
           tprop = vtkTextProperty.New();
           tprop.SetColor(0, 0, 0);
           tprop.ShadowOn();



           vtkCubeAxesActor2D axes;
           axes = vtkCubeAxesActor2D.New();
           axes.SetInput(m_warp.GetOutput()) ;
           axes.SetCamera(this.SParent.m_vtkRenderer.GetActiveCamera());

           //axes.SetLabelFormat("%6.4g");
           axes.SetFlyModeToOuterEdges();
           axes.SetFontFactor(0.8);
           axes.SetAxisTitleTextProperty(tprop);
           axes.SetAxisLabelTextProperty(tprop);
           axes.SetNumberOfLabels(8);
           axes.SetLabelFormat("%8.1f");

     




           vtkAxisActor2D xA2 = axes.GetXAxisActor2D();
           xA2.SetNumberOfMinorTicks(1);

           vtkAxisActor2D zA2 = axes.GetZAxisActor2D();
           zA2.SetNumberOfLabels(3);




           this.SParent.m_vtkRenderer.AddViewProp(axes);


           //this.SParent.m_vtkRenderer.GetActiveCamera().SetFocalPoint(m_Info.OrgPoint[0],m_Info.OrgPoint[1],m_Info.OrgPoint[2]);

           //double[] center = this.SParent.m_vtkRenderer.GetActiveCamera().GetFocalPoint();




           //看来做完这一切之后，还得更新相机的设置啊


           this.SParent.m_vtkRenderer.GetActiveCamera().SetFocalPoint(m_Info.CenterPoint[0], m_Info.CenterPoint[1], m_Info.CenterPoint[2]);




         //  this.SParent.m_vtkRenderer.GetActiveCamera().SetPosition(m_Info.CenterPoint[0], m_Info.CenterPoint[1]+1000, m_Info.CenterPoint[2]);


           //vtkOrientationMarkerWidget omark = vtkOrientationMarkerWidget.New();
           //omark.SetInteractor(this.SParent.m_vtkInteractor);

           //omark.SetOrientationMarker(this.m_Img .m_Actor);

           //omark.SetEnabled(1);
 
       }

       public void CreateTerrain()
       {
           vtkImageData data = m_Info.imgData;

           //主地形的添加



           vtkImageDataGeometryFilter gemo = vtkImageDataGeometryFilter.New();
           gemo.SetInput(data);
           gemo.OutputTrianglesOn();


           vtkTextureMapToPlane tmt = new vtkTextureMapToPlane();
           tmt.SetInput(gemo.GetOutput());

           tmt.SetOrigin(m_TInfo.wsPointX, m_TInfo.wsPointY, m_Info.OrgPoint[2]);
           tmt.SetPoint1(m_TInfo.enPointX, m_TInfo.wsPointY, m_Info.OrgPoint[2]);
           tmt.SetPoint2(m_TInfo.wsPointX, m_TInfo.enPointY, m_Info.OrgPoint[2]);

           tmt.SetSRange(0, 0.99);
           tmt.SetTRange(0,0.99);


           //vtkThresholdTextureCoords tcoords = vtkThresholdTextureCoords.New();
           //tcoords.SetInput(tmt.GetOutput());
           ////tcoords.ThresholdByLower(0.5);
           //tcoords.ThresholdByUpper(0.99);





           vtkWarpScalar m_warp = vtkWarpScalar.New();
           m_warp.SetInput(tmt.GetOutput());

           m_warp.SetScaleFactor(1);
           m_warp.SetNormal(0, 0, 1);
           m_warp.UseNormalOn();

           m_warp.Update();










           double[] dRange = data.GetScalarRange();






           vtkPolyDataNormals normal = vtkPolyDataNormals.New();
           normal.SetInput(m_warp.GetOutput());
           normal.SetFeatureAngle(60);
           normal.SplittingOff();




           //不切割了它，我心里难受，外加有额外的故事啊，郁闷，VTK的纹理真是他妈的弱智


           vtkBox box = vtkBox.New();
           box.SetXMin(m_TInfo.wsPointX, m_TInfo.wsPointY, m_Info.ScalarRange [0]);
           box.SetXMax(m_TInfo.enPointX , m_TInfo.enPointY , m_Info.OrgPoint[1]);


           vtkClipPolyData clip = vtkClipPolyData.New();
           clip.SetInput(normal.GetOutput());
           clip.SetClipFunction(box);
           clip.GenerateClippedOutputOn();
        



       
           m_TerrainMain.m_Lt.SetHueRange(0.6, 0);
           m_TerrainMain.m_Lt.SetSaturationRange(1, 0);
           m_TerrainMain.m_Lt.SetValueRange(0.5, 1);


           //m_TerrainMain.m_Data = normal.GetOutput();

           //m_TerrainMain.m_Mapper.SetInput(normal.GetOutput());


           m_TerrainMain.m_Data = clip.GetOutput();


           m_TerrainMain.m_Mapper.SetInputConnection(clip.GetOutputPort());

           m_TerrainMain.m_Mapper.SetScalarRange(dRange[0],dRange[1]);
           m_TerrainMain.m_Mapper.SetScalarVisibility(1);


           //m_TerrainMain.m_Actor.SetMapper(m_TerrainMain.m_Mapper);




          // m_TerrainMain.m_Actor.SetTexture(m_TInfo.Texture );



           this.SParent.m_vtkRenderer.AddActor(m_TerrainMain.m_Actor);









           //裁切剩下的小兄弟们

           m_Img.m_Data = clip.GetClippedOutput ();

           m_Img.m_Mapper.SetInput(clip.GetClippedOutput());

           m_Img.m_Mapper.SetScalarVisibility(0);
           m_Img.m_Mapper.SetUseLookupTableScalarRange(1);





           m_Img.m_Actor.SetTexture(m_TInfo.Texture);



           this.SParent.m_vtkRenderer.AddActor(m_Img.m_Actor);





           



       }
       public void CreateOutLine()
       {
           vtkOutlineFilter outline = vtkOutlineFilter.New();
           outline.SetInput(m_TerrainMain.m_Data);

           m_OutLine.m_Mapper.SetInput(outline.GetOutput());


           this.SParent.m_vtkRenderer.AddActor(m_OutLine.m_Actor);





          
       }




       //粘贴浓度图的纹理系统

       public void SetTextureCoord(double wsPointX, double wsPointY, double enPointX, double enPointY)
       {

           m_TInfo = new TextureInfo(wsPointX, wsPointY, enPointX, enPointY);

       }

       public void SetTextureFromImage(string pfilename)
       {
           vtkImageReader2 imgReader = vtkImageReader2Factory.CreateImageReader2(pfilename);
           imgReader.SetFileName(pfilename);
           imgReader.Update();

           m_TInfo.TextureImage = imgReader.GetOutput();

        
       }

       public void SetTextureFromGridImage(vtkImageData img)
       {
           double[] drange = img.GetScalarRange();

           vtkLookupTable lt = vtkLookupTable.New();
           lt.SetRange(drange[0], drange[1]);

           m_TInfo.TextureImage = img;
           m_TInfo.Texture.SetLookupTable(lt);
           m_TInfo.Texture.InterpolateOn();


       }

      






       

       

      
    }
}
